package com.yzy.soft.system.freemarker;


import com.yzy.soft.common.persistence.model.TypeMvc;

import com.yzy.soft.system.freemarker.database.Table;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class CodeGenerateUtils {
    private static Logger logger = LoggerFactory.getLogger(CodeGenerateUtils.class);

    private static final String PATH="com.yzy.soft.system.freemarker.template.Template";
    private FileInfo fileInfo;
    private DBInfo dbInfo;
    private Table table;

    public FileInfo getFileInfo() {
        return fileInfo;
    }

    public void setFileInfo(FileInfo fileInfo) {
        this.fileInfo = fileInfo;
    }

    public DBInfo getDbInfo() {
        return dbInfo;
    }

    public void setDbInfo(DBInfo dbInfo) {
        this.dbInfo = dbInfo;
    }

    public Table getTable() {
        return table;
    }

    public void setTable(Table table) {
        this.table = table;
    }

    public CodeGenerateUtils() {

    }

    public List<File> generate(List<TypeMvc> typeMvcs) throws Exception {
        logger.info("CodeGenerateUtils generate:{}",typeMvcs);
        logger.info("CodeGenerateUtils generate1:{}", typeMvcs.size());
        if (null == fileInfo||null == dbInfo) {

            logger.info("1111111111111111111111");
            return null;
        }
        List<File> fileList=new ArrayList<>();
        try {
            for (int i = 0; i < typeMvcs.size(); i++) {
                TypeMvc typeMvc = typeMvcs.get(i);
                logger.info("模板路径是："+PATH + typeMvc.getName());
                TemplateKernel templateKernel = (TemplateKernel) Class.forName(PATH + typeMvc.getName()).newInstance();
                File file= templateKernel.generateFileType(fileInfo, table, dbInfo);

                logger.info("CodeGenerateUtils generate1的返回的文件路径： {}",file.getPath());
                fileList.add(file);
            }
            return fileList;
        } catch (ClassNotFoundException e) {
            logger.error("没有对应模板处理类");
            throw new RuntimeException(e);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("没有对应模板处理类 Exception");
            throw new RuntimeException(e);

        } finally {
            return fileList;
        }
    }
}
 

